/* This file is part of the Crimsoon project
  * Copyright (C) 2009 Eduardo Henrique <eduardohleite@gmail.com>
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
  * License as published by the Free Software Foundation; either
  * version 2 of the License, or (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Library General Public License for more details.
  *
  * You should have received a copy of the GNU Library General Public License
  * along with this library; see the file COPYING.LIB.  If not, write to
  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  * Boston, MA 02110-1301, USA.
*/

#ifndef PLAYPANEL_H
#define PLAYPANEL_H

#include <QWidget>
#include <QList>

#include <seekslider.h>
#include <volumeslider.h>
#include <mediaobject.h>
#include <audiooutput.h>

class QVBoxLayout;
class QHBoxLayout;
class QPushButton;
class QLabel;

class PlayList;
class Track;

enum PlayPanelStatus
{
    PLAYPANEL_READY,
    PLAYPANEL_PLAYING,
    PLAYPANEL_PAUSED,
    PLAYPANEL_STOPPED,
    PLAYPANEL_ERROR
};

enum PlayPanelMode
{
    PLAYPANEL_NORMAL,
    PLAYPANEL_REPEAT,
    PLAYPANEL_REPEAT_ALL,
    PLAYPANEL_SHUFFLE
};

class PlayPanel : public QWidget
{
    Q_OBJECT

public:
    PlayPanel();
    ~PlayPanel();

    void prepare(QString filename);
    void prepare(PlayList *files, qint32 startIndex = 0, bool autoPlay = false);    
    void forceStop();
    void setRepeat()       { mode = PLAYPANEL_REPEAT; }
    void setRepeatAll()    { mode = PLAYPANEL_REPEAT_ALL; }
    void removeRepeat()    { if (mode == PLAYPANEL_REPEAT) mode = PLAYPANEL_NORMAL; }
    void removeRepeatAll() { if (mode == PLAYPANEL_REPEAT_ALL) mode = PLAYPANEL_NORMAL; }
    QString currentFile();
    PlayPanelStatus getStatus() { return status; }
    PlayPanelMode getMode() { return mode; }

signals:
    void playFileChanged(QString filename);
    void playFileNotFound(QString filename);
    void statusChanged();

private:
    QVBoxLayout *mainLayout;
    QHBoxLayout *buttonsLayout;
    QHBoxLayout *sliderLayout;
    QPushButton *playPauseButton;
    QPushButton *stopButton;
    QPushButton *previousButton;
    QPushButton *nextButton;
    QLabel      *time;

    Phonon::SeekSlider *slider;
    Phonon::VolumeSlider *volume;
    Phonon::MediaObject *mediaObject;
    Phonon::AudioOutput *audioOutput;

    QList<Phonon::MediaSource> *playlist;   

    bool autoPlay;

    PlayPanelStatus status;
    PlayPanelMode mode;

    void createWidgets();
    void disableControls();
    void resetPlaylist();    

private slots:
    void onPreviousClicked();
    void onPlayPauseClicked();
    void onStopClicked();
    void onNextClicked();

    void onTick(qint64 time);
    void onMediaStateChanged(Phonon::State state, Phonon::State);
    void onMediaSourceChanged(const Phonon::MediaSource &source);
    void onPlaybackAboutToFinish();
    void onPlaybackFinished();    
};

#endif // PLAYPANEL_H
